//
//  BleSendReceiveTableViewCell.m
//  bleDemo
//
//  Created by wurz on 15/4/14.
//  Copyright (c) 2015年 wurz. All rights reserved.
//

#import "BleSendReceiveTableViewCell.h"

@implementation BleSendReceiveTableViewCell
{
    UILabel *_titleLab;
    UILabel *_timeLab;
    UILabel *_contextLab;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        // Initialization code
        [self initSubview];
    }
    return self;
}

#pragma mark 初始化视图
- (void)initSubview
{
    //时间
    _timeLab = [[UILabel alloc] init];
    _timeLab.font = [UIFont systemFontOfSize:14];
    [self addSubview:_timeLab];
    
    //标题
    _titleLab = [[UILabel alloc] init];
    _titleLab.font = [UIFont systemFontOfSize:14];
    [self addSubview:_titleLab];
    
    //内容
    _contextLab = [[UILabel alloc] init];
    _contextLab.font = [UIFont systemFontOfSize:12];
    _contextLab.numberOfLines = 0;//多行
    [self addSubview:_contextLab];
}

-(void)setBleData:(BleSendReceiveData *)data
{
    CGFloat avatarX = 10;
    CGFloat spacing = 10; //控件间距
    
    CGFloat textX = avatarX;
    CGFloat textY = CGRectGetMaxY(CGRectZero) + spacing;
    CGFloat textWidth = self.frame.size.width - 2 * spacing;

    CGSize textSize = [data.title boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    CGRect textRect = CGRectMake(textX, textY, textSize.width, textSize.height);
    _titleLab.text = data.title;
    _titleLab.frame = textRect;
    
    CGSize timeSize = [data.time boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    CGFloat createAtX = [UIScreen mainScreen].bounds.size.width-timeSize.width-10;
    CGFloat createAtY = _titleLab.frame.origin.y;
    CGRect createAtRect = CGRectMake(createAtX, createAtY, timeSize.width, timeSize.height);
    _timeLab.text = data.time;
    _timeLab.frame = createAtRect;
    
    CGFloat titleTextX = textX;
    CGFloat titleTextY = CGRectGetMaxY(_titleLab.frame) + spacing;
    CGFloat titleWidth = self.frame.size.width - 2 * spacing;
    CGSize titleSize = [data.context boundingRectWithSize:CGSizeMake(titleWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    CGRect titleRect = CGRectMake(titleTextX, titleTextY, titleSize.width, titleSize.height);
    //CGRect titleRect = CGRectMake(30+titleTextX, titleTextY, 300, 10);
    _contextLab.text = data.context;
    _contextLab.frame = titleRect;
    
    if ([data.title isEqualToString:@"发送:"]) {
        _titleLab.textColor = [UIColor blueColor];
        _contextLab.textColor = [UIColor blueColor];
        _timeLab.textColor = [UIColor blueColor];
    }
    else
    {
        _titleLab.textColor = [UIColor redColor];
        _contextLab.textColor = [UIColor redColor];
        _timeLab.textColor = [UIColor redColor];
    }
    
    
    _height = CGRectGetMaxY(_contextLab.frame) + spacing;
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
